import datetime

from analyze import Analyze
from conf import FileConf
from parser import Parser

my_conf = FileConf("./confs/log.ini", 100 * 1024 * 1024)

my_parser = Parser(my_conf)


# class MyAnalyze(Analyze):
#
#     func_dict = {}
#
func_dict = {}
pat = r"(?P<date>.*?) (?P<time>.*?) " \
          r"(?P<type>\w+)\((?P<main_id>\d+)\) (?P<sub_type>.*),(?P<sub_id>\d+)( (?P<action>.*))?"


@my_parser.add_analyser(pat)
def handle(match, ori_line):
    if match:
        accept_type = {"OnRequest", "aE_Req"}

        request = f'{match.main_id}_{match.sub_id}'  # 唯一ID
        request_type = match.type  # 此次请求的类型
        request_subtype = match.sub_type.replace(",", ".")  # 此次请求的细节

        time = "{} {}".format(match.date, match.time)
        # request_action = match.action  # 请求的行为

        func_dict.setdefault(request, {})
        if request_type in accept_type:
            func_dict[request].setdefault(request_type, {})
            func_dict[request][request_type].setdefault(request_subtype, {})
            start_req = func_dict[request][request_type][request_subtype]
            start_req.setdefault("start_time", [])
            start_req["start_time"].append(time)
        else:
            if request_type == "aE_RespOk":
                request_type = "aE_Req"
            elif request_type == "endAll_Ok":
                request_type = "OnRequest"

            stop_req = func_dict[request][request_type][request_subtype]
            stop_req.setdefault("stop_time", [])
            stop_req["stop_time"].append(time)
    else:
        print(ori_line, "can not match")
        print("*" * 50)


if __name__ == '__main__':
    start = datetime.datetime.now()
    my_parser.parse()
    delta = (datetime.datetime.now() - start).total_seconds()
    print(delta)
